<?php 
	class  Survey extends Model{
		private $_SurveyId;
		private $_Description;
		private $_Options = array();
		private $_Active;

		function __construct($arrInitialValues = null){
			parent::__construct();
			
			if (isset($arrInitialValues['survey_id']))
				$this->setSurveyId($arrInitialValues['survey_id']);
			else
				$this->setSurveyId(0);

			if (isset($arrInitialValues['description']))
				$this->setDescription($arrInitialValues['description']);
			else
				$this->setDescription('');
			
			if (isset($arrInitialValues['options']))
				$this->setOptions($arrInitialValues['options']);
			else
				$this->setOptions(array());
			
			if (isset($arrInitialValues['active']))
				$this->setActive($arrInitialValues['active']);
			else
				$this->setActive(0);
		}
		
		public function setSurveyId($value){
			$this->_SurveyId = $value;
		}
		
		public function getSurveyId(){
			return $this->_SurveyId;
		}
		
		public function setDescription($value){
			$this->_Description = $value;
		}
		
		public function getDescription(){
			return $this->_Description;
		}
		
		public function setActive($value){
			$this->_Active = $value;
		}
		
		public function getActive(){
			return $this->_Active;
		}
		
		public function setOptions($value){
			$this->_Options = $value;
		}

		public function getOptions(){
			return $this->_Options;
		}
		
		public function deactivateOthers(){
			$query = '
				UPDATE surveys
				SET active = 0, 
					last_activated = 0
				WHERE survey_id <> ' . $this->getSurveyId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public function deactivateIfHasNoOptions($SurveyId){
			$query = '
				UPDATE surveys AS su
				SET su.active = 0
				WHERE 
					su.survey_id = ' . $SurveyId . '
					AND (SELECT COUNT(1)
			        FROM options AS op
			        WHERE op.survey_id = ' . $SurveyId . '
			        AND op.active = 1) = 0';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public static function getAll(){
			$query = '
				SELECT *
				FROM surveys';
				
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function getSurvey(){
			$query = '
				SELECT *
				FROM surveys
				WHERE survey_id = ' . $this->getSurveyId();

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}

		public function obtainOptions(){
			$query = '
				SELECT *
				FROM options
				WHERE survey_id = ' . $this->_SurveyId;
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();			
		}
		
		public function update(){
			$query = '
				UPDATE surveys
				SET description = "' . $this->getDescription() . '",  
				active = ' . $this->getActive() . ' 
				WHERE survey_id = ' . $this->getSurveyId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		} 
		
		public function create(){
			$query = '
				INSERT INTO surveys
				(description, active)
				VALUES
				("' . $this->getDescription() . '", ' . $this->getActive() . ')';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getLastIdInserted();
		}
		
		public function activate(){
			$query = '
				UPDATE surveys
				SET	active = 1,
					last_activated = 1
				WHERE survey_id = ' . $this->getSurveyId();

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public function deactivate(){
			$query = '
				UPDATE surveys
				SET	active = 0
				WHERE survey_id = ' . $this->getSurveyId();
		
			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
	}
?>